package com.digital.herbarium.Controller;

import com.digital.herbarium.Entity.BaseLibrary;
import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.BaseLibraryService;
import com.digital.herbarium.task.InitBaseLibrary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/baselibrary")
public class BaseLibraryController {

    @Autowired
    BaseLibraryService baseLibraryService;
    @Value("${static.excelurl}")
    private String staticurl;

    @GetMapping("getBaseLibraryInfo")
    @Log("baselibrary")
    public ResponseResult getBaseLibraryInfo(BaseLibrary baseLibrary, @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                             @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
      // List<BaseLibrary> baseLibraryInfo = baseLibraryService.getBaseLibraryInfo(baseLibrary, pageSize,  pageNum);
        return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryInfo(baseLibrary, pageSize,  pageNum));
    }

    @GetMapping("getManagerBaseLibraryInfo")
    @CheckToken()
    @Log("baselibrary")
    public ResponseResult getManagerBaseLibraryInfo(BaseLibrary baseLibrary, @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                             @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
        // List<BaseLibrary> baseLibraryInfo = baseLibraryService.getBaseLibraryInfo(baseLibrary, pageSize,  pageNum);
        return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryInfo(baseLibrary, pageSize,  pageNum));
    }

    @GetMapping("getBaseLibraryTree")
    @Log("baselibrary")
    public ResponseResult getBaseLibraryTree(BaseLibrary baseLibrary){
//        return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryTree1());
        if(InitBaseLibrary.treeList != null && InitBaseLibrary.treeList.size() > 0){
            return ResponseResult.SUCCESS().setData(InitBaseLibrary.treeList);
        }else{
           // return ResponseResult.SUCCESS().setData("是空的");
            return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryTree1());
        }

    }

    @GetMapping("getManagerBaseLibraryTree")
    @CheckToken()
    @Log("baselibrary")
    public ResponseResult getManagerBaseLibraryTree(BaseLibrary baseLibrary){
//        return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryTree1());
        if(InitBaseLibrary.treeList != null && InitBaseLibrary.treeList.size() > 0){
            return ResponseResult.SUCCESS().setData(InitBaseLibrary.treeList);
        }else{
            // return ResponseResult.SUCCESS().setData("是空的");
            return ResponseResult.SUCCESS().setData(baseLibraryService.getBaseLibraryTree1());
        }

    }



    @PostMapping("addBaseLibrary")
    @CheckToken()
    public  ResponseResult addBaseLibrary(BaseLibrary baseLibrary){
        baseLibraryService.addBaseLibrary(baseLibrary);
        return ResponseResult.SUCCESS().setData("添加成功");
    }

    @PostMapping("editBaseLibrary")
    @CheckToken()
    public ResponseResult editBaseLibrary(BaseLibrary baseLibrary){
        baseLibraryService.editBaseLibrary(baseLibrary);
        return  ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("delBaseLibrary")
    @CheckToken()
    public ResponseResult delBaseLibrary(String id){
        baseLibraryService.delBaseLibrary(id);
        return  ResponseResult.SUCCESS().setData("删除成功");
    }


    @PostMapping("uploadExcel")
    @CheckToken()
    public  ResponseResult uploadExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        baseLibraryService.addExcel(file,request,staticurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }



}
